export default function loadScript(key, url) {
  return new Promise((resolve, reject) => {
      if (window[key]) {
          resolve(window[key])
      } else {
          var script = document.createElement('script'),
          head = document.getElementsByTagName('head')[0];
          script.charset = 'utf-8'
          script.type = 'text/javascript'
          script.src = url
          script.onerror = reject
          if (script.addEventListener) {
            script.addEventListener('load', () => {
              resolve(window[key])
            })
          } else if (script.attachEvent) {
            script.attachEvent('onreadystatechange', () => {
              var target = window.event.srcElement;
              if (target.readyState == 'loaded') {
                resolve(window[key])
              }
            })
          }

          head.appendChild(script)
      }
  })
}
